package com.wdh.kjs.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.Adapter.InvestmentDetailFourAdapter;
import com.wdh.kjs.Adapter.ListViewForScrollView;
import com.wdh.kjs.R;
import com.wdh.kjs.activity.InvestmentDetailActivity;
import com.wdh.kjs.activity.LoginActivity;
import com.wdh.kjs.activity.investrecordActivity;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel;
import com.wdh.kjs.util.DateUtilChange;
import com.wdh.kjs.util.ToastUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dingh on 2016/4/21.
 */
public class InvestmentDetailFourFragment extends Fragment implements View.OnTouchListener, View.OnClickListener {

    private InvestmentDetailFourAdapter investmentDetailAdapter;
    private List<HashMap<String, String>> datas;
    private PullToRefreshListView investrecordlist;
    private JsonModel jsonModel;
    private ScrollView zpf_scroll;
    private Button zpf_button;
    private LinearLayout ii_fgfour_investdetail;
    private LinearLayout ii_fgfour_investdetail_gone;
    private TextView tv_fgfour_weidenglu;
    private Intent intent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_investmentdetail_four, container, false);
        initView(view);
        if (InvestmentDetailActivity.loginToken.equals("")){
            ii_fgfour_investdetail.setVisibility(View.GONE);
            ii_fgfour_investdetail_gone.setVisibility(View.VISIBLE);
            tv_fgfour_weidenglu.setOnClickListener(this);
        }else {
            initweb();;
        }
//        initweb();
        return view;
    }

    private void initweb() {
        RequestParams params = new RequestParams();

        params.addBodyParameter("itemId", InvestmentDetailActivity.itemId);
        params.addBodyParameter("page", 1 + "");
        params.addBodyParameter("rows", "10");
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_Invest_DETAIL, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {


                Gson gson = new Gson();
                jsonModel = gson.fromJson(responseInfo.result, JsonModel.class);

                if (jsonModel.success) {

                    item();
                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

                ToastUtils.showShort(getActivity(), "网络异常！");
            }
        });

    }

    private void item() {
        investmentDetailAdapter=new InvestmentDetailFourAdapter(getActivity(),initListViewData());
        investrecordlist.setAdapter(investmentDetailAdapter);
    }

    private void initView(View view) {
        investrecordlist = (PullToRefreshListView) view.findViewById(R.id.investrecordlist);
        zpf_scroll = (ScrollView) getActivity().findViewById(R.id.zpf_scroll);
        zpf_button = (Button) view.findViewById(R.id.zpf_button);
        ii_fgfour_investdetail = (LinearLayout) view.findViewById(R.id.ii_fgfour_investdetail);
        ii_fgfour_investdetail_gone = (LinearLayout) view.findViewById(R.id.ii_fgfour_investdetail_gone);
        tv_fgfour_weidenglu = (TextView) view.findViewById(R.id.tv_fgfour_weidenglu);
        investrecordlist.setOnTouchListener(this);
        zpf_button.setOnClickListener(this);

    }

    private List<HashMap<String, String>> initListViewData() {
        datas = new ArrayList<>();
        for(int i=0;i<jsonModel.getInvestUsers().size();i++){
            HashMap<String, String> hashMap = new HashMap<>();
            BigDecimal mybigDecimal =new BigDecimal(jsonModel.getInvestUsers().get(i).getTotalInvestment()+"").setScale(2, RoundingMode.HALF_UP);
            hashMap.put("tv_item_id_four_phone", jsonModel.getInvestUsers().get(i).getNickName());
            hashMap.put("tv_item_id_four_money", "￥"+mybigDecimal);
            String time = DateUtilChange.timestamp2String(jsonModel.getInvestUsers().get(i).getCreatedStamp(), DateUtilChange.DEFAULT_SECOND_FORMAT);
            hashMap.put("tv_item_id_four_time", time);
            datas.add(hashMap);
        }
        return datas;

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            zpf_scroll.requestDisallowInterceptTouchEvent(false);
            investrecordlist.requestDisallowInterceptTouchEvent(true);
        } else
            zpf_scroll.requestDisallowInterceptTouchEvent(true);

        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zpf_button:
                 intent = new Intent(getActivity(), investrecordActivity.class);
                intent.putExtra("itemId", InvestmentDetailActivity.itemId);
                startActivity(intent);
                break;
            case R.id.tv_fgfour_weidenglu:
                intent = new Intent(getActivity(), LoginActivity.class);
                intent.putExtra("state", "0");
                startActivity(intent);
                break;
        }
    }
}
